package com.ctw.mapmycity.rest;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

import org.springframework.context.support.AbstractApplicationContext;

import com.ctw.mapmycity.hbase.controller.AccountController;
import com.ctw.mapmycity.model.Account;
import com.ctw.mapmycity.spring.HBaseContextFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Path("/account")
public class AccountRS{
	
	@Context
	UriInfo uriInfo;
	
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public Account registerAcct(Account account) {
		
		AbstractApplicationContext context = HBaseContextFactory.getContext();
		AccountController controller = context.getBean(AccountController.class);
		
		// check if the account exists
		if (controller.getAccount(account.getEmail()).getEmail() == null) {
			controller.saveAccount(account);
			account = controller.getAccount(account.getEmail());
		} else {
			account = null;
		}
		
		return account;
	}
	
	@GET
	@Path ("auth")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes("text/plain")
	public Account authenticate(@QueryParam("email") String email, @QueryParam("token") String token) {
		token = token != null ? token.trim() : ""; 			
		
		AbstractApplicationContext context = HBaseContextFactory.getContext();
		AccountController controller = context.getBean(AccountController.class);
		
		Account account = controller.getAccount(email);
		
		if (account.getEmail() != null) {
			
			if (!account.getToken().equalsIgnoreCase(token)) {
				// token does not matches
				account = null;
			} else {
				// clear pass
				account.setPassword("");
				
				// TODO generate new token
				account.setToken(token);
			}
		} 
		
		return account;
	}
	
	@GET
	@Path ("accounts")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Account> getAccounts() {
		AbstractApplicationContext context = HBaseContextFactory.getContext();
		AccountController controller = context.getBean(AccountController.class);
		
		return controller.findAllAccounts();
	}
	
	@GET
	@Path ("{email}")
	@Consumes("text/plain")
	@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
	public Account getAccount(@PathParam("email") String email) {
		AbstractApplicationContext context = HBaseContextFactory.getContext();
		AccountController controller = context.getBean(AccountController.class);
		
		return controller.getAccount(email);
	}
	
	@POST
	@Path ("delete/{email}")
	@Consumes("text/plain")
	public String deleteAccount(@PathParam("email") String email) {
		AbstractApplicationContext context = HBaseContextFactory.getContext();
		AccountController controller = context.getBean(AccountController.class);
		
		controller.deleteAccount(email);
		
		return "OK";
	}
	
	@GET
	@Path ("subpath3/{postKey}")
	@Consumes("text/plain")
	@Produces(MediaType.APPLICATION_JSON)
	public Account getPostDetails(@PathParam("postKey") int userName) {

		ObjectMapper mapper = new ObjectMapper();
		
//		try {
//			JsonNode jnode = mapper.readTree(param);
//			System.out.println(jnode.get("token"));
//			System.out.println(jnode.get("status"));
//			System.out.println(jnode.get("authType"));
//				
//		} catch (Exception e1) {
//			e1.printStackTrace();
//		}
		
		ObjectNode json = mapper.createObjectNode();
		
		json.put("token", "ksjdhfwi4u4yrwirwu6g4");
		json.put("status", 200);
		json.put("authType", "FB");
		
		Account acc = new Account() {{
			setEmail("welintoncoradini@gmail.com");
		}};
		
		return acc;
	}
}
